package com.universest.swordholder.ui.widget
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Shapes
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.universest.swordholder.task.AbstractTask
import com.universest.swordholder.task.Task
import com.universest.swordholder.ui.theme.SwordHolderTheme
import androidx.compose.runtime.getValue
import androidx.compose.ui.text.style.TextAlign
import com.alibaba.fastjson.JSON
import com.universest.swordholder.tools.toFloat
import com.universest.swordholder.tools.toNumberProgress
import com.universest.swordholder.tools.toPercentage

enum class ProgressMode{
    Percentage,
    Number
}

@Composable
fun TaskProgressWidget(modifier: Modifier = Modifier, progressMode: ProgressMode = ProgressMode.Percentage, task: AbstractTask<*, *>){
    Column(modifier = modifier) {
        Text(text = task.name,modifier = Modifier.padding(top = 10.dp, start = 10.dp))
        val message by task.message.collectAsStateWithLifecycle("")
        val progress by task.progress.collectAsStateWithLifecycle(Pair(0,1))
        Box(modifier = Modifier
            .padding(start = 10.dp, end = 10.dp, top = 10.dp)
            .border(1.dp,
                shape = RoundedCornerShape(5.dp),
                color = MaterialTheme.colorScheme.primary
            )
            .fillMaxWidth()
            .height(20.dp)) {
            Box(modifier = Modifier
                .background(MaterialTheme.colorScheme.primary, shape = RoundedCornerShape(5.dp))
                .fillMaxHeight()
                .fillMaxWidth(progress.toFloat())){

            }
        }
        Row(modifier = Modifier
            .heightIn(max = 100.dp)
            .padding(start = 10.dp,top = 5.dp, bottom = 5.dp,end = 10.dp)) {
            val scrollState = rememberScrollState()
            val parsedMessage = messageParse(message)
            LaunchedEffect(parsedMessage) {
                scrollState.animateScrollTo(scrollState.maxValue)
            }
            Text(text = parsedMessage,
                style = MaterialTheme.typography.bodySmall.copy(
                    color = MaterialTheme.typography.bodySmall.color.copy(alpha = 0.7f)
                ),
                modifier = Modifier.weight(1f).verticalScroll(scrollState)
            )
            Text(text = when(progressMode){
                ProgressMode.Percentage -> progress.toPercentage()
                ProgressMode.Number -> progress.toNumberProgress()
            },style = MaterialTheme.typography.bodySmall.copy(
                color = MaterialTheme.typography.bodySmall.color.copy(alpha = 0.7f)
            , textAlign = TextAlign.End),
                modifier = Modifier.width(40.dp))
        }
    }
}

private fun messageParse(message: String): String{
    return when{
        message.startsWith("aiProgress") -> {
            val jsonObject = JSON.parseObject(message.replaceFirst("aiProgress:",""))
            val deepThought = jsonObject.getString("deepThought")
            val content = jsonObject.getString("content")
            return if(content.isEmpty()){
                "深度思考：$deepThought"
            }else{
                "结果：$content"
            }
        }
        else -> message
    }
}

@Composable
@Preview
fun TaskProgressWidgetPreview(){
    SwordHolderTheme {
        TaskProgressWidget(
            modifier = Modifier.fillMaxWidth().height(200.dp),
            task = Task<String, String>("测试任务", 1,"测试任务信息") {
                updateMessage("测试信息...")
                ""
            })
    }
}